package com.supreme.securityserver.security;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.supreme.commonentity.mysql.primary.SysUser;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Set;

@NoArgsConstructor
@Data
public class AuthUser implements UserDetails {

    private static final long serialVersionUID = 1L;

    private String token;

    private Long userId;

    private String username;

    private String password;

    private String userCode;

    private Set<PermitAuthority> authorities;

    /**
     * 登录时间
     */
    private Long loginTime;

    /**
     * 过期时间
     */
    private Long expireTime;

    /**
     * 登录IP地址
     */
    private String ipaddr;

    /**
     * 登录地点
     */
    private String loginLocation;

    /**
     * 浏览器类型
     */
    private String browser;

    /**
     * 操作系统
     */
    private String os;

    public AuthUser(SysUser sysUser) {
        this.userId = sysUser.getUserId();
        this.username = sysUser.getUsername();
        this.password = sysUser.getPassword();
        this.userCode = sysUser.getUserCode();
    }

    public AuthUser(SysUser sysUser, Set<PermitAuthority> permissions) {
        this.userId = sysUser.getUserId();
        this.username = sysUser.getUsername();
        this.password = sysUser.getPassword();
        this.userCode = sysUser.getUserCode();
        this.authorities = permissions;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @JSONField(serialize = false)
    @JsonIgnore
    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @JSONField(serialize = false)
    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JSONField(serialize = false)
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JSONField(serialize = false)
    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JSONField(serialize = false)
    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}
